/**
 * 应用商品
 */
$(document).ready(function() {
	var appGoodsDialog;
	var bindModelData = {
		appGoodsForm:{name:"",code:"",appTypeId:"",appModuleId:"",AppMenuId:"",accessPath:"",summary:"",icon:"",salesGroup:""},
		appGoodsSearch:{name:""}
	}
	var appGoodsFormModel,appGoodsSearchFormModel,appGoodsFormValidator;
	function init(){
		initSearchForm();
		initAppGoodsForm();
		if($("#search").data("LBSFormSearch")===undefined){
			$("#search").LBSFormSearch({
				searchBar:true,
				searchHandle:searchHandle,
				resetHandle:resetSearchHandle,
				custom:[{name:"add",value:"添加",click:function(e){addAppGoodsHandle(e);},spriteCssClass:"k-icon k-i-plus"}]
			});
		}
		
		if($("#app_goods_grid").data("kendoGrid")===undefined){
			$("#app_goods_grid").kendoGrid({
				autoBind:true,
		        dataSource: getGridDataSource({
		        	read:{
		        		url:"/system/appGoods/find",
		        	},
		        	model:{
		        		id:"id",
		        		fields:{
		        			id:{type:"string"},
		        			name:{type:"string"},
		        			icon:{type:"string"},
		        			appTypeName:{type:"string"},
		        			appModuleName:{type:"string"},
		        			appMenuName:{type:"string"},
		        			summary:{type:"string"},
		        			accessPath:{type:"string"},
		        			createTime:{type:"date"},
		        			modifyTime:{type:"date"}
		        		}
		        	}
		        }),
		        sortable: true,
		        selectable: true,
		        editable: false,
		        resizable: true,
		        pageable: true,
		        dataBinding: function() {
		            record = (this.dataSource.page() -1) * this.dataSource.pageSize();
		        },
		        columns: [
		            {title: "序号",template: "#= ++record #",width: 50},
		            {field: "id",title: "编号",hidden:true}, 
		            {field: "name",title: "名称"}, 
		            {field: "icon",title: "应用图片",template: '<img src="#= icon #" alt="image" style="width:auto;height:25px;"/>'},
		            {field: "appTypeName",title: "应用类型"},
		            {field: "appModuleName",title: "应用模块"},
		            {field: "appMenuName",title: "应用菜单"},
		            {field: "accessPath",title: "管理路径"},
		            {field: "summary",title: "应用简介"},
		            {field: "salesGroup",title: "销售分类",values:getStaticData("app_sales_group")},
		            {field: "createTime",title: "创建时间",format: "{0: yyyy-MM-dd HH:mm:ss}"},
		            {title: "操作" ,command: [{name: "更新", click:modifyAppGoodsHandle}],width: 160}
		        ]
		    });
		}
	}
	function initSearchForm(){
		if($("input[name='name']",$("#search")).data("kendoMaskedTextBox")===undefined){
			$("input[name='name']",$("#search")).kendoMaskedTextBox();
		}
		appGoodsSearchFormModel = kendo.observable(bindModelData.appGoodsSearch);
		kendo.bind("#search", appGoodsSearchFormModel);
	}
	function initAppGoodsForm(){
		appGoodsFormValidator = $("#app_goods_form").kendoValidator().data("kendoValidator");
		if($("#app_goods_form").data("LBSDialog")===undefined){
			appGoodsDialog = $("#app_goods_form").LBSDialog({
				height:450,
				width:550,
				deactivate:resetFormHandle,
				bbars:{
					save:saveFormHandle
				}
			});
		}
		if($("input[name='name']",$("#app_goods_form")).data("kendoMaskedTextBox")===undefined){
			$("input[name='name']",$("#app_goods_form")).kendoMaskedTextBox();
		}
		if($("input[name='type']",$("#app_goods_form")).data("kendoMaskedTextBox")===undefined){
			$("input[name='type']",$("#app_goods_form")).kendoMaskedTextBox();
		}
		//图片上传未处理
		if($("input[name='files']",$("#app_goods_form")).data("kendoUpload")===undefined){
			$("input[name='files']",$("#app_goods_form")).kendoUpload({
				async:{
					saveUrl:"/common/file/upload?purposeCode=app_stroe_image"
				},
				localization: {
		            select: "上传应用图片"
		        },
				multiple:false,//只能传一个文件
				success:function(e){
					var response = e.response;
					$("div.k-upload").find("img").remove();
					$("ul.k-upload-files").after("<img src='"+response.result[0].url+"' style='width:268px;height:200px;'>");
					$("ul.k-upload-files").hide();
					appGoodsFormModel.set("icon",response.result[0].url);
				},
				error:function(e){
					$("div.k-upload").find("img").remove();
					$("ul.k-upload-files").show();
					console.log(e.XMLHttpRequest);
					var data = JSON.parse(e.XMLHttpRequest.response);
					$("span.k-filename","li.k-file-error").html(data.message);
				}
			});
		}
		if($("input[name='accessPath']",$("#app_goods_form")).data("kendoMaskedTextBox")===undefined){
			$("input[name='accessPath']",$("#app_goods_form")).kendoMaskedTextBox();
		}
		if ($("input[name='appType']", $("#app_goods_form")).data("kendoComboBox") === undefined) {
			$("input[name='appType']", $("#app_goods_form")).kendoComboBox({
				valuePrimitive : true,
				dataTextField : "name",
				dataValueField : "id",
				dataSource : getListDataSource({
					url : "/system/appType/dataList"
				}),
				placeholder : "请选择应用类型...",
			});
		}
		if ($("input[name='module']", $("#app_goods_form")).data("kendoComboBox") === undefined) {
			$("input[name='module']", $("#app_goods_form")).kendoComboBox({
				valuePrimitive : true,
				dataTextField : "name",
				dataValueField : "id",
				dataSource : getListDataSource({
					url : "/system/appModule/dataList"
				}),
				placeholder : "请选择应用模块...",
			});
		}
		if ($("input[name='menu']", $("#app_goods_form")).data("kendoComboBox") === undefined) {
			$("input[name='menu']", $("#app_goods_form")).kendoComboBox({
				valuePrimitive : true,
				dataTextField : "name",
				dataValueField : "id",
				dataSource : getListDataSource({
					url : "/system/appMenu/dataList"
				}),
				placeholder : "请选择应用菜单...",
			});
		}
		if ($("input[name='salesGroup']", $("#app_goods_form")).data("kendoComboBox") === undefined) {
			$("input[name='salesGroup']", $("#app_goods_form")).kendoComboBox({
				valuePrimitive : true,
				dataTextField : "name",
				dataValueField : "id",
				dataSource: getListDataSource({url:"/common/dict/app_sales_group/data"}),
				placeholder : "请选择销售分类..."
			});
		}
	}
	function main(){
		init();
	}
	function searchHandle(){
		$("#app_goods_grid").data("kendoGrid").dataSource.read({content:kendo.stringify(appGoodsSearchFormModel)});
	}
	function resetSearchHandle(){
		appGoodsSearchFormModel = kendo.observable(bindModelData.appGoodsSearch);
		kendo.bind("#search", appGoodsSearchFormModel);
	}
	function resetFormHandle(){
		appGoodsFormModel = kendo.observable(bindModelData.appGoodsForm);
		kendo.bind("#app_goods_form", appGoodsFormModel);
	}
	function saveFormHandle(){
		var url = "/system/appGoods/save";
		if(this.paramters!==undefined){url = this.paramters.url}
		if(appGoodsFormValidator.validate()){
			httpRequestHandle({
				url:url,
				data:{content:kendo.stringify(appGoodsFormModel)}
			}).done(function(response){
				$("#app_goods_grid").data("kendoGrid").dataSource.read();
			});
			this.close();
		}else{
			LBSNotification({message:"检查表格字段是否为空"});
		}
		delete this.paramters;
	}
	function addAppGoodsHandle(){
		$("div.k-upload","#app_goods_form").find("img").remove();
		appGoodsFormModel = kendo.observable(bindModelData.appGoodsForm);
		kendo.bind("#app_goods_form", appGoodsFormModel);
		appGoodsDialog.open();
	}
	function modifyAppGoodsHandle(e){
		var grid = $("#app_goods_grid").data("kendoGrid");
		var tr = $(e.target).closest("tr");
		var data = grid.dataItem(tr);
		console.log(data.icon);
		if(data.icon!==""&&data.icon!==null){
			$("div.k-upload","#app_goods_form").find("img").remove();
			$("div.k-dropzone","#app_goods_form").after("<img src='"+data.icon+"' style='width:268px;height:200px;'>");
		}
		appGoodsFormModel = kendo.observable(data);
		kendo.bind("#app_goods_form", appGoodsFormModel);
		appGoodsDialog.open({paramters:{url:"/system/appGoods/update"}});
	}
	main();
});